package com.liuda066.forumssm.common;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/8/31 23:32
 */
public class AppResult<T> {
    // 状态码
    //⽆论是否为空必须序列化
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private long code;
    // 状态描述
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    // 结果数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    // 构造⽅法
    public AppResult() { }
    public AppResult(long code, String message) {
        this(code, message, null);
    }
    public AppResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    //成功
    public  static <T> AppResult<T> success(){
        return new AppResult<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }
    public  static <T> AppResult<T> success(T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }
    public  static <T> AppResult<T> success(String message,T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(),message,data);
    }

    //失败
    public static <T> AppResult <T> failed(){
        return new AppResult<>(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
    }
    public static <T> AppResult <T> failed(String message){
        return new AppResult<>(ResultCode.FAILED.getCode(),message);
    }
    public static <T> AppResult <T> failed(T data){
        return new AppResult<>(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage(),data);
    }
    public static <T> AppResult <T> failed(ResultCode resultCode){
        return new AppResult<>(resultCode.getCode(),resultCode.getMessage());
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
